﻿using DocBot.Objects;
using DocBot.Util;
using DocBot.Util.Collections;

using GalaSoft.MvvmLight;

using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Xml.Serialization;

namespace DocBot.ViewModel
{
	public class LogViewModel : ViewModelBase, INotifyPropertyChanged
	{
		#region Members
		private static string configFileName = "Log.xml";
		private LocalDataBase _database = new LocalDataBase();
		ThreadSafeObservableCollection<LogItem> _items = new ThreadSafeObservableCollection<LogItem>();
		private LogItem _selectedLogItem = new LogItem();
		#endregion

		#region Properties
		public ThreadSafeObservableCollection<LogItem> Items
		{
			get
			{
				return _items;
			}
			set
			{
				_items = value;
			}
		}

		public LogItem Last
		{
			get { return _selectedLogItem; }
			set
			{
				if (value != this._selectedLogItem)
					this._selectedLogItem = value;
				NotifyPropertyChanged();
			}
		}

		#endregion

		#region Events
		new public event PropertyChangedEventHandler PropertyChanged;

		// This method is called by the Set accessor of each property.
		// The CallerMemberName attribute that is applied to the optional propertyName
		// parameter causes the property name of the caller to be substituted as an argument.
		private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion

		#region Construction
		public LogViewModel()
		{
			if (IsInDesignMode)
			{
				LoadLog(configFileName);
				if (_items.Count == 0)
					for (int i = 0; i < 50; ++i)
					{
						_items.Add(new LogItem { Message = _database.GetRandomArtistName, Time = DateTime.Now });
					}
			}
			else
			{
				LoadLog(configFileName);
			}
		}

		~LogViewModel()
		{
			if (!IsInDesignMode)
				if (Settings.Instance.SaveLog)
					SaveLog(configFileName);
		}
		#endregion

		#region Methods
		public void Add(String information)
		{
			LogItem l = new LogItem { Message = information, Time = DateTime.Now };
			Last = l;
			_items.Add(l);
		}


		public void LoadLog(String configFileName)
		{

			if (File.Exists(Settings.Instance.configFilePath + "\\" + configFileName))
			{
				XmlSerializer deserializer = new XmlSerializer(typeof(ThreadSafeObservableCollection<LogItem>));
				TextReader textReader = new StreamReader(Settings.Instance.configFilePath + "\\" + configFileName);
				ThreadSafeObservableCollection<LogItem> _object = (ThreadSafeObservableCollection<LogItem>)deserializer.Deserialize(textReader);
				textReader.Close();
				if (_object == null)
				{
					_object = new ThreadSafeObservableCollection<LogItem>();
				}
				_items = _object;
			}
		}

		private void SaveLog(String configFileName)
		{
			if (_items.Count > 0)
			{
				XmlSerializer s = new XmlSerializer(typeof(ThreadSafeObservableCollection<LogItem>));
				TextWriter writer = new StreamWriter(Settings.Instance.configFilePath + "\\" + configFileName);
				s.Serialize(writer, _items);
				writer.Close();
			}
		}
		#endregion
	}
}
